home *** CD-ROM | disk | FTP | other *** search
/ VRML Browsing & Building Cyberspace / VRML - Browsing and Building Cyberspace.iso / txt2wrl / txt2wrl.cxx
C/C++ Source or Header  |  1995-08-03  |  8KB  |  313 lines

  1. /*
  2. Copyright Layne Thomas(lthomas@cs.uah.edu) April 23,1995
  3. this code is a sloppy hack but it works
  4.  
  5. This source is subject to the terms and stuff of the GNU public license.
  6.  
  7. *******IMPORTANT STUFF*********
  8. Code reads in a txt file, and outputs the 3d representation in coord3 format
  9. usage: txt2wrl <infilename> <z height(default is 2)> 
  10. output is to stdout
  11.  
  12. Compile with :  g++ txt2wrl.cxx -o txt2wrl
  13.  
  14. note: txt2wrl txt files have to have a blank line as the first line in the file!
  15. This is cuz I'm a lazy butt and should change it in the future
  16.  
  17. Go to http://cs.uah.edu/~lthomas/vrml for more information
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. unsigned int matrix_size;
  25. int yunit,xunit;
  26. unsigned int *real_matrix;
  27. int points;
  28. int pointsb;
  29. int Z=2;
  30.  
  31. unsigned matrix(int x, int y){
  32.     unsigned result=real_matrix[(unsigned int)(x+(y*xunit))];
  33.     return result;
  34. }
  35.  
  36. void set_matrix(int x, int y,unsigned int value){
  37.     real_matrix[(unsigned int)(x+(y*xunit))]=value;
  38. }
  39.  
  40. void clean(char *dastring,unsigned int length){
  41.       int cx;
  42.       for (cx=0;cx<length;cx++){
  43.       dastring[cx]=' ';
  44.       }
  45. }
  46.  
  47. void get_line(FILE *infile, char *dest){
  48.      int c=0;
  49.  
  50.      dest[c]=0;
  51.      while((dest[c-1] != '\n')&&(!feof(infile))){
  52.         fscanf(infile,"%c",&dest[c]);
  53. //Detect tabs(8 spaces to a tab)
  54.         if (dest[c]==9) {
  55.            dest[c]=' ';
  56.            c+=7;
  57.         }
  58.         c++;
  59.      }
  60.      if (feof(infile)) dest[c+1]=0;
  61.      else dest[c]=0;
  62.  
  63. }
  64.  
  65. int get_ranges(char *filename,int &lines,int &xrange,int &zrange){
  66.     FILE *infile;
  67.     char crap[2048];
  68.     int longestline;
  69.  
  70.     if ((infile = fopen(filename, "rt"))== NULL){
  71.         printf("Error opening %s\n",filename);
  72.         return 1;
  73.     }
  74.  
  75.     lines=0;
  76.     longestline=0;
  77.  
  78.     while (!feof(infile)) {
  79.           get_line(infile,crap);
  80.           if (strlen(crap) > longestline) {
  81.          longestline=strlen(crap);
  82.           }
  83.           lines++;
  84.     }
  85.     fclose(infile);
  86.     xrange=longestline;
  87.     zrange=lines;
  88.     return 0;
  89. }
  90.  
  91. int txt2rem(char *filename){
  92.     FILE *infile;
  93.     char crap[2048];
  94.  
  95.     if ((infile = fopen(filename, "rt"))== NULL){
  96.         printf("Error opening %s\n",filename);
  97.         return 0;
  98.     }
  99.  
  100.     clean(crap,2048);
  101.     while (!feof(infile)) {
  102.           get_line(infile,crap);
  103.           printf("#%s",crap);
  104.     }
  105.     fclose(infile);
  106.     return 0;
  107. }
  108.  
  109.  
  110. void parse_line(char *data,int curline,int xrange,int zrange){
  111.      int x;
  112.      int first=1;
  113.  
  114.      if (strlen(data)==0) return;
  115.      for (x=0;x<strlen(data);x++) {
  116.          if ((data[x] != ' ')&&(data[x]!='\n')) {
  117.             set_matrix(x,curline,1);
  118.          }
  119.          else {
  120.             set_matrix(x,curline,0);
  121.          }
  122.      }
  123. }
  124.  
  125. void clear_matrix(){
  126.      unsigned int x;
  127.      for (x=0;x<matrix_size/4;x++){
  128. //         printf("X=%d\n",x);
  129.          real_matrix[x]=0;
  130.      }
  131. }
  132.  
  133. void print_matrix(int xrange,int zrange){
  134.      int x,y;
  135.  
  136.      for (y=0;y<zrange;y++) {
  137.          printf("\n#");
  138. //         printf("\n  #(%d)",y);
  139.          for (x=0;x<xrange;x++) {
  140.              if (matrix(x,y)==0) printf(" ");
  141.              else if (matrix(x,y)==1) printf("+");
  142.              else printf("@");
  143. //             printf("%d",matrix(x,y));
  144.          }
  145.      }
  146. }
  147.  
  148. void print_line(int xrange, int line) {
  149.      int x;
  150.  
  151.      printf("\n  #(%d)",line);
  152.      for (x=0;x<xrange;x++) {
  153.          printf("%d",matrix(x,line));
  154.      }
  155. }
  156.  
  157. int horizontal_line(int x,int y){
  158.     if ((matrix(x+1,y)!=0)&&(matrix(x-1,y)!=0)) {
  159. //Is horizontal, but is it just a line or a junction?
  160.        if ((matrix(x,y-1)!=0)||(matrix(x,y+1)!=0)) return 0;
  161.        return 1;
  162.     }
  163.     return 0;
  164. }
  165.  
  166. int vertical_line(int x,int y){
  167.     if ((matrix(x,y+1)!=0)&&(matrix(x,y-1)!=0)){
  168.        if ((matrix(x-1,y)!=0)||(matrix(x+1,y)!=0)) return 0;
  169.        return 1;
  170.     }
  171.     return 0;
  172. }
  173.  
  174. int is_point(int x,int y){
  175.     if (vertical_line(x,y)) return 0;
  176.     if (horizontal_line(x,y)) return 0;
  177.     return 1;
  178. }
  179.  
  180. int get_points(int xrange, int zrange){
  181.     int x,y,count;
  182.  
  183.     count=2; //Skip 0 cuz 0=empty,1=wall
  184.     for (y=0;y<zrange;y++){
  185.         for (x=0;x<xrange;x++){
  186.             if (matrix(x,y)!=0) {
  187.                if (is_point(x,y)) {
  188.                   set_matrix(x,y,count);
  189.                printf("\t\t%d %d %d,\n",x,0,y);
  190.                printf("\t\t%d %d %d,\n",x,Z,y);
  191.                    count+=2;
  192.                } else {
  193.                    set_matrix(x,y,1); 
  194.                }
  195.             } else {
  196.                 set_matrix(x,y,0);
  197.             } 
  198.         }
  199.     }
  200. }
  201.  
  202. int build_hor_walls(int xrange, int zrange){
  203.     int x,y;
  204.     int node1,node2;
  205.  
  206.     for (y=0;y<zrange;y++){
  207.         for (x=0;x<xrange;x++){
  208.             if ((matrix(x,y)>1)&&((matrix(x+1,y)!=0)||(matrix(x-1,y)!=0))){
  209.                node1=x;
  210.                while (matrix(x+1,y)>0) {
  211.                      x++;
  212.                      node2=x;
  213.                }
  214.  printf("\t\t%d,%d,%d,%d,-1,\n",matrix(node1,y)-2,matrix(node1,y)+1-2,
  215. matrix(node2,y)+1-2,matrix(node2,y)-2);
  216.             }
  217.         }
  218.     } 
  219. }
  220.  
  221. int build_ver_walls(int xrange, int zrange){
  222.     int x,y;
  223.     int node1,node2;
  224.  
  225.     for (x=0;x<xrange;x++){
  226.         for (y=0;y<zrange;y++){
  227.             if ((matrix(x,y)>1)&&((matrix(x,y+1)!=0)||(matrix(x,y-1)!=0))){
  228.                node1=y;
  229.                while (matrix(x,y+1)>0) {
  230.                      y++;
  231.                      node2=y;
  232.                }
  233. //              printf("Line (%d,%d)(%d)-(%d,%d)(%d) matrix is %d\n",x,node1,matrix(x,node1),x,node2,matrix(x,node2),matrix(x,y));
  234.  printf("\t\t%d,%d,%d,%d,-1,\n",matrix(x,node1)-2,matrix(x,node1)+1-2,
  235. matrix(x,node2)+1-2,matrix(x,node2)-2);
  236.             }
  237.         }
  238.     } 
  239. }
  240.  
  241. int main(int argc,char *argv[]) {
  242.     char *data=(char *)malloc(2048);
  243.         char *filename=(char *)malloc(1024);
  244.     int lines,zrange,xrange;
  245.     int cur_line;
  246.     FILE *infile;
  247.  
  248.         if (argc != 3) {
  249.            printf("usage: txt2wrl <infilename> <z height(default 2)>\n");
  250.            return 0;
  251.         }
  252.         strcpy(filename,argv[1]);
  253.     get_ranges(filename,lines,xrange,zrange);
  254.         if (atoi(argv[2]) != 0) Z=atoi(argv[2]);
  255.         
  256.     printf("#VRML V1.0 ascii\n");
  257.     printf("#There are %d lines, ",lines);
  258.     printf("X range is %d to %d, ",-xrange/2,xrange/2);
  259.     printf("Z range is %d to %d\n",-zrange/2,zrange/2);
  260.  
  261.         yunit=zrange;
  262.         xunit=xrange;
  263.         matrix_size=(xrange)*(zrange)*2+(xrange*2);//2 for int, other for extra
  264.         real_matrix=(unsigned int *)malloc(matrix_size*2);
  265.         if (real_matrix==NULL) {
  266.            printf("Not enough memory to initialize matrix\n");
  267.            return -1;
  268.         }
  269.         printf("#Matrix %p,size is %d(yunit=%d)\n",real_matrix,matrix_size,yunit);
  270.         clear_matrix();
  271.  
  272.         printf("#opening %s\n",filename);
  273.     txt2rem(filename);
  274.     printf("\n");
  275.     printf("Separator {\n");
  276.         printf("  Scale { scaleFactor 1.0 1.0 3.2 }\n");
  277.     printf("  Material { emissiveColor 0.3 0.3 0.0 }\n");
  278.     printf("  Coordinate3 {\n");
  279.     printf("     point [\n");
  280.  
  281.     if ((infile = fopen(filename, "rt"))== NULL){
  282.         printf("Error opening %s\n",filename);
  283.     }
  284.     clean(data,2048);
  285.     points=0;
  286.         pointsb=0;
  287.     cur_line=0;
  288. //Get informational data
  289.         printf("#parsing\n");
  290.     while (!feof(infile)){
  291.           get_line(infile,data);
  292.               parse_line(data,cur_line,xrange,zrange);
  293.           cur_line++;
  294.     }
  295.         get_points(xrange,zrange);
  296.         printf("        0 0 0]\n"); 
  297.         print_matrix(xrange,cur_line);
  298.         fclose(infile);
  299.  
  300.     printf("\n  }\n");
  301.     printf("  IndexedFaceSet {\n");
  302.     printf("     coordIndex [ \n");
  303.         build_hor_walls(xrange,zrange);
  304.         printf("\n#          Vertical walls\n");
  305.         build_ver_walls(xrange,zrange);
  306.         printf("\n\t\t0,0,0,0,-1 ] \n");
  307.     printf("  }\n");
  308.     printf("}\n");
  309.     return 0;
  310. }
  311.  
  312.  
  313.